import util from './utils/index.js';
const regeneratorRuntime = util.regeneratorRuntime
import siteInfo from './siteinfo.js';
const prefix = siteInfo.prefix
import {
  userModel
} from "./apis/index"
module.exports = {
  data: {},
  async submitFormId(e) {
    await getApp().submitFormId(e.detail.formId)
  },
  onShow() {
  },
  computedPage(pageFiled, total) {
    let page = this.data[pageFiled]
    let totalPage = Math.ceil(total / page.pageSize)
    page.totalPage = totalPage
    page.page++
    this.data[pageFiled] = page
  },
  loadMoreData(pageFiled, method) {
    let page = this.data[pageFiled]
    if (page.page > page.totalPage) {
      return false;
    }
    method && method()
  },
  async getuuid() {
    let userInfo = prefix.getStorageSync('userinfo') || {};
    if (!userInfo.id) {
      userInfo = await this.getUserInfo()
    }
    return userInfo.id
  },
  getUserBaseInfo() {
    let userInfo = prefix.getStorageSync('userinfo') || {};
    if (!userInfo.id) {
      userInfo = this.getUserInfo()
    }
    let {
      name,
      avatar
    } = userInfo
    return {
      name,
      avatar
    }
  },
  async getUserInfo() {
    let userInfo = await userModel.getUserInfo()
    wx.setStorage({
      key: 'userinfo',
      data: userInfo
    });
    return userInfo
  },
  async onGetUserInfo(e) {
    if (e.detail.errMsg == 'getUserInfo:ok') {
      let {
        avatarUrl,
        nickName
      } = e.detail.userInfo
      let callBack = e.currentTarget.dataset.callback
      let call = this[callBack]
      if(!prefix.getStorageSync('haveUserInfo')){
        let data = await userModel.haveUserInfo()
        if(data.data){
          call && call(e)
        }else{
          userModel.saveUserInfo({
            avatar: avatarUrl,
            name: nickName
          }).then(res => {
           prefix.setStorage({
             key:'haveUserInfo',
             data:true
           })
           call && call(e)
          })
        }
      }else{
        call && call(e)
      }
    }
  },
  getShareInfo({type='1',sid=''}={}){
    userModel.getShareInfo({type,sid}).then(res=>{
      this.data.share_data = res
    })
  }
}